package ThreadDemo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2025-03-20
 * Time: 09:58
 */

/*class MyThread3 extends Thread{
    //自定义变量
    public static boolean isQuit = false;

    @Override
    public void run() {
        while (!isQuit){
            System.out.println("线程工作中！");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("线程工作完毕！");

    }
}*/

public class ThreadDemo9 {
    private static boolean isQuit = false;

    public static void main(String[] args) {
        /*//创建Thread对象
        Thread t = new MyThread3();
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //MyThread3.isQuit = true;//如果在这个位置，则最后的运行结果不确定
        System.out.println("让t线程结束运行");
        MyThread3.isQuit = true;//在这个位置，则是main方法中的打印先执行，然后执行run方法中的内容*/


        Thread t = new Thread(
                ()->{
                    while (!isQuit){
                        System.out.println("我是一个线程，正在工作中...");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("线程执行完毕！");
                }
        );
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("让t线程结束");
        isQuit = true;
    }
}
